<?php
/**
 * Created by .
 * User: luogan
 * Date: 2022/1/17
 */

namespace app\admin\service;


use app\exception\ErrorException;
use app\model\MenuModel;

class MenuService extends BaseService
{
    /**
     * 获取菜单列表
     * @param $data
     * @return array
     * @throws ErrorException
     */
    public function getList($data)
    {
        $result = (new MenuModel())->getMenuTreeList($data);

        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    /**
     * 新增菜单
     * @param $data
     * @return MenuModel|\think\Model
     * @throws ErrorException
     * @throws \ErrorException
     */
    public function create($data)
    {

        $result = MenuModel::create($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('新增菜单：'.$data['title']. '菜单id '.$result);
        return $result;
    }

    /**
     * 编辑菜单
     * @param $data
     * @return MenuModel|\think\Model
     * @throws ErrorException
     * @throws \ErrorException
     */
    public function update($data)
    {

        $result = MenuModel::where('id',$data['id'])->update($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('编辑菜单：菜单id '.$data['id']);
        return $result;
    }

    /**
     * 获取菜单详情
     * @param $id
     * @return MenuModel|array|mixed|\think\Model
     */
    public function getInfo($id)
    {
        $result = MenuModel::find($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    /**
     * 删除菜单
     * @param $id
     * @return bool
     * @throws ErrorException
     * @throws \ErrorException
     */
    public function delete($id)
    {
        $result = MenuModel::destroy($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
        admin_log('删除菜单：菜单'.$id);
    }
}
